package com.example.studyllm.service.rag;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.studyllm.entity.rag.RagMessage;
import com.example.studyllm.mapper.rag.RagMessageMapper;
import com.example.studyllm.service.base.BaseServiceImpl;
import com.example.studyllm.utils.BeanUtils;
import com.example.studyllm.vo.base.PageResultVO;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollectionUtil;

/**
 * <p>
 *   service实现类
 * </p>
 *
 * @author kw
 * @since 2025-02-05 15:12:50.622740
 */
@Service
public class RagMessageService extends BaseServiceImpl<RagMessageMapper, RagMessage> {


    public Boolean addRagMessage(RagMessage ragMessage) {
        return this.save(ragMessage);
    }

    public Boolean updateRagMessageById(RagMessage ragMessage) {
        return this.updateById(ragMessage);
    }

    public PageResultVO<RagMessage> getPage(RagMessage pageParam) {

        LambdaQueryWrapper<RagMessage> lambda = new QueryWrapper<RagMessage>().lambda();
        this.buildCondition(lambda, pageParam);
        lambda.orderBy(true, false, RagMessage::getId);              
        PageResultVO<RagMessage> resultPage = BeanUtils.changePage(RagMessage.class, this.page(new Page<>(pageParam.getPageNo(), pageParam.getPageSize()), lambda));
        return resultPage;
    }
    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<RagMessage> lambda, RagMessage param) {
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getId()),RagMessage::getId, param.getId());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getUserId()),RagMessage::getUserId, param.getUserId());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getChatId()),RagMessage::getChatId, param.getChatId());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getUserName()),RagMessage::getUserName, param.getUserName());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getMessage()),RagMessage::getMessage, param.getMessage());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getKnowledgeId()),RagMessage::getKnowledgeId, param.getKnowledgeId());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getModelId()),RagMessage::getModelId, param.getModelId());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getTokens()),RagMessage::getTokens, param.getTokens());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getPromoteTokens()),RagMessage::getPromoteTokens, param.getPromoteTokens());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getCtime()),RagMessage::getCtime, param.getCtime());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getUtime()),RagMessage::getUtime, param.getUtime());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getDeleteFlag()),RagMessage::getDeleteFlag, param.getDeleteFlag());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getCby()),RagMessage::getCby, param.getCby());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getUby()),RagMessage::getUby, param.getUby());
        
    }
}